# ------------------------------------------------------------------------------
# Replication Materials
# 
# title: Eliciting Beliefs as Distributions in Online Surveys
# journal: Political Analysis
# authors: Lucas Leemann, Richard Traunmüller, and Lukas Stoetzer
# date: August 2020
# ------------------------------------------------------------------------------




### Balance Check

# Tab A1

source("00_DataCleaner_symmetric.R") # Richard Code to clean Data

data$quantile <- ifelse(is.na(data$Q80)==F & is.na(data$Q95)==F & is.na(data$Q97)==F, 1, 0)
data$interval.w <- ifelse(is.na(data$Q115)==F & is.na(data$Q116_1)==F & is.na(data$Q117_1)==F, 1, 0)
data$interval.n <- ifelse(is.na(data$Q132)==F & is.na(data$Q290_4)==F & is.na(data$Q291_1)==F, 1, 0)
data$manski <- ifelse(is.na(data$Q121)==F & is.na(data$Q122)==F & is.na(data$Q125_1)==F & is.na(data$Q123)==F & is.na(data$Q126_1)==F, 1, 0)
data$bins <- ifelse(is.na(data$q1_2024)==F & is.na(data$q1_2529)==F & is.na(data$q1_3034)==F & is.na(data$q1_3539)==F & is.na(data$q1_4044)==F &
                    is.na(data$q1_4549)==F & is.na(data$q1_5054)==F & is.na(data$q1_5559)==F & is.na(data$q1_6064)==F & is.na(data$q1_6569)==F, 1, 0)

data$female <- ifelse(data$Q83=="Female", 1, 0)
#data$age <- droplevels(data$Q84)
data$age <- as.numeric(as.character(data$Q84))
data$university <- ifelse(data$Q85=="University degree", 1, 0)
data$polint <- ifelse(data$Q86=="", NA, data$Q86)
data$polint <- as.character(data$polint)
data$polint[data$polint=="No at all interested"] <- 1
data$polint[data$polint=="Hardly interested"] <- 2
data$polint[data$polint=="Quite interested"] <- 3
data$polint[data$polint=="Very interested"] <- 4
data$polint <- as.numeric(data$polint)

balance.test.1.b <- data %>% group_by(quantile) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.2.b <- data %>% group_by(interval.w) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.3.b <- data %>% group_by(interval.n) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.4.b <- data %>% group_by(manski) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.5.b <- data %>% group_by(bins) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))




balance.test.1 <- data %>% group_by(quantile) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.2 <- data %>% group_by(interval.w) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.3 <- data %>% group_by(interval.n) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.4 <- data %>% group_by(manski) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.5 <- data %>% group_by(bins) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))


aa <- xtable(round(rbind(balance.test.1.b[2,2:6], 
                   balance.test.1[2,2:6], 
                   balance.test.2.b[2,2:6],
                   balance.test.2[2,2:6],
                   balance.test.3.b[2,2:6],
                   balance.test.3[2,2:6],
                   balance.test.4.b[2,2:6],
                   balance.test.4[2,2:6],
                   balance.test.5.b[2,2:6],
                   balance.test.5[2,2:6]), 2))

print(xtable(aa, caption="Balance Check"), 
      booktabs = T, include.rownames=FALSE,
      file="out/fig/Tab_A1.tex")

# Tab A2

source("00_DataCleaner_symmetric_variance.R") # Richard Code to clean Data

data$quantile <- ifelse(is.na(data$Q80)==F & is.na(data$Q95)==F & is.na(data$Q97)==F, 1, 0)
data$interval.w <- ifelse(is.na(data$Q115)==F & is.na(data$Q116_1)==F & is.na(data$Q117_1)==F, 1, 0)
data$interval.n <- ifelse(is.na(data$Q132)==F & is.na(data$Q290_4)==F & is.na(data$Q291_1)==F, 1, 0)
data$manski <- ifelse(is.na(data$Q121)==F & is.na(data$Q122)==F & is.na(data$Q125_1)==F & is.na(data$Q123)==F & is.na(data$Q126_1)==F, 1, 0)
data$bins <- ifelse(is.na(data$q1_2024)==F & is.na(data$q1_2529)==F & is.na(data$q1_3034)==F & is.na(data$q1_3539)==F & is.na(data$q1_4044)==F &
                      is.na(data$q1_4549)==F & is.na(data$q1_5054)==F & is.na(data$q1_5559)==F & is.na(data$q1_6064)==F & is.na(data$q1_6569)==F, 1, 0)

data$female <- ifelse(data$Q83=="Female", 1, 0)
#data$age <- droplevels(data$Q84)
data$age <- as.numeric(as.character(data$Q84))
data$university <- ifelse(data$Q85=="University degree", 1, 0)
data$polint <- ifelse(data$Q86=="", NA, data$Q86)
data$polint <- as.character(data$polint)
data$polint[data$polint=="No at all interested"] <- 1
data$polint[data$polint=="Hardly interested"] <- 2
data$polint[data$polint=="Quite interested"] <- 3
data$polint[data$polint=="Very interested"] <- 4
data$polint <- as.numeric(data$polint)



balance.test.1.b <- data %>% group_by(quantile) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.2.b <- data %>% group_by(interval.w) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.3.b <- data %>% group_by(interval.n) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.4.b <- data %>% group_by(manski) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.5.b <- data %>% group_by(bins) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))


balance.test.1 <- data %>% group_by(quantile) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.2 <- data %>% group_by(interval.w) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.3 <- data %>% group_by(interval.n) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.4 <- data %>% group_by(manski) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.5 <- data %>% group_by(bins) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))




aa <- xtable(round(rbind(balance.test.1.b[2,2:6], 
                         balance.test.1[2,2:6], 
                         balance.test.2.b[2,2:6],
                         balance.test.2[2,2:6],
                         balance.test.3.b[2,2:6],
                         balance.test.3[2,2:6],
                         balance.test.4.b[2,2:6],
                         balance.test.4[2,2:6],
                         balance.test.5.b[2,2:6],
                         balance.test.5[2,2:6]), 2))

print(xtable(aa, caption="Balance Check"), 
      booktabs = T, include.rownames=FALSE,
      file="out/fig/Tab_A2.tex")



# Tab A3

source("00_DataCleaner_asymmetric.R") # Richard Code to clean Data

data$quantile <- ifelse(is.na(data$Q80)==F & is.na(data$Q95)==F & is.na(data$Q97)==F, 1, 0)
data$interval.w <- ifelse(is.na(data$Q115)==F & is.na(data$Q116_1)==F & is.na(data$Q117_1)==F, 1, 0)
data$interval.n <- ifelse(is.na(data$Q132)==F & is.na(data$Q290_4)==F & is.na(data$Q291_1)==F, 1, 0)
data$manski <- ifelse(is.na(data$Q121)==F & is.na(data$Q122)==F & is.na(data$Q125_1)==F & is.na(data$Q123)==F & is.na(data$Q126_1)==F, 1, 0)
data$bins <- ifelse(is.na(data$q1_2529)==F & is.na(data$q1_3034)==F & is.na(data$q1_3539)==F & is.na(data$q1_4044)==F &
                      is.na(data$q1_4549)==F & is.na(data$q1_5054)==F & is.na(data$q1_5559)==F & is.na(data$q1_6064)==F & is.na(data$q1_6569)==F &
                      is.na(data$q1_7074)==F & is.na(data$q1_7579)==F & is.na(data$q1_8084)==F, 1, 0)

data$female <- ifelse(data$Q83=="Female", 1, 0)
#data$age <- droplevels(data$Q84)
data$age <- as.numeric(as.character(data$Q84))
data$university <- ifelse(data$Q85=="University degree", 1, 0)
data$polint <- ifelse(data$Q86=="", NA, data$Q86)
data$polint <- as.character(data$polint)
data$polint[data$polint=="No at all interested"] <- 1
data$polint[data$polint=="Hardly interested"] <- 2
data$polint[data$polint=="Quite interested"] <- 3
data$polint[data$polint=="Very interested"] <- 4
data$polint <- as.numeric(data$polint)

balance.test.1.b <- data %>% group_by(quantile) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.2.b <- data %>% group_by(interval.w) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.3.b <- data %>% group_by(interval.n) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.4.b <- data %>% group_by(manski) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.5.b <- data %>% group_by(bins) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))

balance.test.1 <- data %>% group_by(quantile) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.2 <- data %>% group_by(interval.w) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.3 <- data %>% group_by(interval.n) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.4 <- data %>% group_by(manski) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.5 <- data %>% group_by(bins) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))



aa <- xtable(round(rbind(balance.test.1.b[2,2:6], 
                         balance.test.1[2,2:6], 
                         balance.test.2.b[2,2:6],
                         balance.test.2[2,2:6],
                         balance.test.3.b[2,2:6],
                         balance.test.3[2,2:6],
                         balance.test.4.b[2,2:6],
                         balance.test.4[2,2:6],
                         balance.test.5.b[2,2:6],
                         balance.test.5[2,2:6]), 2))

print(xtable(aa, caption="Balance Check"), 
      booktabs = T, include.rownames=FALSE,
      file="out/fig/Tab_A3.tex")



# Tab A4



source("00_DataCleaner_asymmetric_variance.R") # Richard Code to clean Data

data$quantile <- ifelse(is.na(data$Q80)==F & is.na(data$Q95)==F & is.na(data$Q97)==F, 1, 0)
data$interval.w <- ifelse(is.na(data$Q115)==F & is.na(data$Q116_1)==F & is.na(data$Q117_1)==F, 1, 0)
data$interval.n <- ifelse(is.na(data$Q132)==F & is.na(data$Q290_4)==F & is.na(data$Q291_1)==F, 1, 0)
data$manski <- ifelse(is.na(data$Q121)==F & is.na(data$Q122)==F & is.na(data$Q125_1)==F & is.na(data$Q123)==F & is.na(data$Q126_1)==F, 1, 0)
data$bins <- ifelse(is.na(data$q1_2024)==F & is.na(data$q1_2529)==F & is.na(data$q1_3034)==F & is.na(data$q1_3539)==F & is.na(data$q1_4044)==F &
                      is.na(data$q1_4549)==F & is.na(data$q1_5054)==F & is.na(data$q1_5559)==F & is.na(data$q1_6064)==F & is.na(data$q1_6569)==F, 1, 0)

data$female <- ifelse(data$Q83=="Female", 1, 0)
#data$age <- droplevels(data$Q84)
data$age <- as.numeric(as.character(data$Q84))
data$university <- ifelse(data$Q85=="University degree", 1, 0)
#data$polint <- droplevels(data$Q86)
data$polint <- ifelse(data$Q86=="", NA, data$Q86)
data$polint <- as.character(data$polint)
data$polint[data$polint=="No at all interested"] <- 1
data$polint[data$polint=="Hardly interested"] <- 2
data$polint[data$polint=="Quite interested"] <- 3
data$polint[data$polint=="Very interested"] <- 4
data$polint <- as.numeric(data$polint)



balance.test.1.b <- data %>% group_by(quantile) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.2.b <- data %>% group_by(interval.w) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.3.b <- data %>% group_by(interval.n) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.4.b <- data %>% group_by(manski) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
balance.test.5.b <- data %>% group_by(bins) %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))

balance.test.1 <- data %>% group_by(quantile) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.2 <- data %>% group_by(interval.w) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.3 <- data %>% group_by(interval.n) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.4 <- data %>% group_by(manski) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))
balance.test.5 <- data %>% group_by(bins) %>% summarize(N=n(), female=sd(female, na.rm=T), age=sd(age, na.rm=T), educ=sd(university, na.rm=T), polint=sd(polint, na.rm=T))




aa <- xtable(round(rbind(balance.test.1.b[2,2:6], 
                         balance.test.1[2,2:6], 
                         balance.test.2.b[2,2:6],
                         balance.test.2[2,2:6],
                         balance.test.3.b[2,2:6],
                         balance.test.3[2,2:6],
                         balance.test.4.b[2,2:6],
                         balance.test.4[2,2:6],
                         balance.test.5.b[2,2:6],
                         balance.test.5[2,2:6]), 2))

print(xtable(aa, caption="Balance Check"), 
      booktabs = T, include.rownames=FALSE,
      file="out/fig/Tab_A4.tex")
# 
# 
# 
# source("00_DataCleaner_nachtrag_sym.R") # Richard Code to clean Data
# 
# data$quantile.noc <- ifelse(is.na(data$Q37)==F & is.na(data$Q39)==F & is.na(data$Q41)==F, 1, 0)
# 
# data$female <- ifelse(data$Q83=="Female", 1, 0)
# #data$age <- droplevels(data$Q84)
# data$age <- as.numeric(as.character(data$Q84))
# data$university <- ifelse(data$Q85=="University degree", 1, 0)
# data$polint <- ifelse(data$Q86=="", NA, data$Q86)
# data$polint <- as.character(data$polint)
# data$polint[data$polint=="No at all interested"] <- 1
# data$polint[data$polint=="Hardly interested"] <- 2
# data$polint[data$polint=="Quite interested"] <- 3
# data$polint[data$polint=="Very interested"] <- 4
# data$polint <- as.numeric(data$polint)
# 
# 
# 
# balance.test.6 <- data %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
# 
# source("00_DataCleaner_nachtrag_asym.R") # Richard Code to clean Data
# data$quantile.noc <- ifelse(is.na(data$Q80)==F & is.na(data$Q95)==F & is.na(data$Q97)==F, 1, 0)
# 
# data$female <- ifelse(data$Q83=="Female", 1, 0)
# #data$age <- droplevels(data$Q84)
# data$age <- as.numeric(as.character(data$Q84))
# data$university <- ifelse(data$Q85=="University degree", 1, 0)
# data$polint <- ifelse(data$Q86=="", NA, data$Q86)
# data$polint <- as.character(data$polint)
# data$polint[data$polint=="No at all interested"] <- 1
# data$polint[data$polint=="Hardly interested"] <- 2
# data$polint[data$polint=="Quite interested"] <- 3
# data$polint[data$polint=="Very interested"] <- 4
# data$polint <- as.numeric(data$polint)
# 
# balance.test.6.b <- data %>% summarize(N=n(), female=mean(female, na.rm=T), age=mean(age, na.rm=T), educ=mean(university, na.rm=T), polint=mean(polint, na.rm=T))
# 
